/**
 * 作者: sessionliang
 * 时间: 2018-10-22
 * 描述:  报名记录页面
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');
	var WindowTools = require('WindowTools_Core');
	var StorageTools = require('StorageTools_Core');

	//base service
	var baseService = require('Base_Service');
	var registerService = require('Register_Service');

	var enumHelper = require('Common_Enum');

	//下拉刷新基类
	var pullToRefreshBase;
	//skin-默认为default
	var skin = 'default';
	//下拉刷新对象
	var pullToRefresh;
	//以下几个是测试加载更多,没有更多数据功能的
	//当前页
	var currpage = 0;
	//每页大小
	var pageSize = 10;
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		skin = WindowTools.getExtraDataByKey('skin') || skin;
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js',
			'js/libs/mustache.min.js'
		], function() {
			initPullToRefreshBySkin(skin);
		});

	}
	/**
	 * @description 初始化时通过skin来决定使用哪一种下拉刷新
	 * 注意；初始化下拉刷新请在初始化时使用,重复使用无效
	 * 这里面是异步引入
	 * @param {String} skin
	 */
	function initPullToRefreshBySkin(skin) {
		var generatePullToRefreshCallback = function(targetPullToRefresh) {
			pullToRefreshBase = targetPullToRefresh;
			initPullRefreshList();
		};
		if(skin === 'default') {
			require.async('PullToRefresh_Base_Default_Core', generatePullToRefreshCallback);
		} else if(skin === 'type0') {
			require.async('PullToRefresh_Base_Type0_Core', generatePullToRefreshCallback);
		} else {
			//其它皮肤都需要引入css
			CommonTools.importFile('css/RayApp/RayApp.PullToRefresh.css');
			if(skin === 'type1') {
				require.async('PullToRefresh_Base_Type1_Core', generatePullToRefreshCallback);
			} else if(skin === 'type1_material1') {
				require.async('PullToRefresh_Base_Type1__Material1_Core', generatePullToRefreshCallback);
			} else {
				console.error("错误:传入的下拉刷新皮肤错误,超出范围!");
			}
		}

	}
	/**
	 * @description 测试添加数据
	 * 真实情况添加的数据要根据接口返回数据映射
	 * @param {Number} count 数量
	 * @param {Boolean} isPullDown 是否是下拉刷新
	 */
	function appendData(count, isPullDown) {
		isPullDown = isPullDown || false;
		var fragment = document.createDocumentFragment();
		var offset =  currpage - 1;
		registerService.getApplys(offset, pageSize, function(retval) {
			for(var i = 0; i < retval.List.length; i++) {
				var item = retval.List[i];
				var li;
				li = document.createElement('li');
				li.className = 'mui-table-view-cell';
				li.setAttribute("data-id", item.ApplyID);
				li.innerHTML += '<div class="mui-slider-right mui-disabled">' +
					'<a class="mui-btn mui-btn-red">删除</a>'; +
				'</div>';
				li.innerHTML += '<div class="mui-slider-handle mui-navigate-right" style="padding-right: 30px;">' +
					'<div class="mui-table">' +
					'<div class="mui-table-cell mui-col-xs-7">' +
					'<p class="mui-ellipsis" style="font-size: 16px;color: #616065;">' + item.DrivingTitle + '<p>' +
					'<p class="mui-h6 mui-ellipsis">' + enumHelper.drivingLicenseTypeFormat(item.DrivingLicenseType) + ' ' + enumHelper.classTypeFormat(item.ClassType) + '</p>' +
					'</div>' +
					'<div class="mui-table-cell mui-col-xs-2 mui-text-right">' +
					'<span class="mui-h5">' + enumHelper.orderStatusTypeFormat(item.Status) + '</span>' +
					'</div>' +
					'</div>' +
					'</div>'
				fragment.appendChild(li);
			}
			var dataContainer = document.getElementById('listdata');
			//添加-下拉刷新时先清除数据
			if(isPullDown) {
				dataContainer.innerHTML = '';
			}
			dataContainer.appendChild(fragment);
			initListeners();
			resetState(isPullDown, retval.List.length);
		}, function() {
			resetState(true, 0);
		});
	}

	/**
	 * @description 监听
	 */
	function initListeners() {
		mui("#listdata").off("tap", "li.mui-table-view-cell").on("tap", "li.mui-table-view-cell", function() {
			var id = this.getAttribute("data-id");
			WindowTools.createWin(null, "apply_detail.html", {
				'mid': id
			}, {});
		});
	}

	/**
	 * @description 重置状态
	 * @param {Boolean} isPullDown 是否是上拉加载
	 */
	function resetState(isPullDown, returnDataCount) {
		if(isPullDown) {
			pullToRefresh.endPullDownToRefresh();
			pullToRefresh.refresh();
		} else {
			if(returnDataCount >= pageSize) {
				pullToRefresh.endPullUpToRefresh(false);
			} else {
				pullToRefresh.endPullUpToRefresh(true);
			}
		}
	}
	/**
	 * @description 初始化下拉刷新
	 * 这时候,基类对象已经可以了
	 */
	function initPullRefreshList() {

		var pullUpRefreshCallback = function() {
			var self = this;
			console.log("pull up");
			//当前页++
			currpage++;
			appendData(pageSize, false);
		};
		var pullDownRefreshCallback = function() {
			var self = this;
			console.log("pull down");
			currpage = 1;
			appendData(pageSize, true);

		};
		var element = '#pullrefresh';

		//初始化下拉刷新
		pullToRefresh = pullToRefreshBase.initPullToRefresh({
			//下拉有关
			down: {
				callback: pullDownRefreshCallback,
			},
			//down为null表示不要下拉刷新
			//down: null,
			//上拉有关
			up: {
				//是否自动上拉加载-初始化是是否自动
				auto: true,
				//距离底部高度(到达该高度即触发)
				offset: 100,
				//是否隐藏那个加载更多动画,达到默认加载效果
				show: true,
				callback: pullUpRefreshCallback
			},
			element: element
		});
	}
});